package cn.fbs.service;

import cn.hutool.core.io.FileUtil;
import cn.fbs.dao.dm.DMCodePhotoAlbumMapper;
import cn.fbs.model.TAtlasInfo;
import cn.fbs.util.FileToPdfUtils;
import cn.fbs.util.WordToPdfUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 安逸
 * @date 2021/1/26 - 16:10
 **/
@Slf4j
@Service
public class PhotoAlbumService {
    @Value("${resource.locations}")
    private String resourceLocations;
    @Autowired
    private DMCodePhotoAlbumMapper tCodePhotoAlbumMapper;


    //更改图册信息，数据类型为图册数据才更新
    //更改图册信息
    public Map<String,Object> updatePhotoAlbumWithDataType(String id,String path){
        //定义返回值
        Map<String,Object> resultMap = new HashMap<>();

        //组织参数
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("id",id);
        paramMap.put("path",path);

        int i = tCodePhotoAlbumMapper.updatePhotoAlbumWithDataType(paramMap);
 System.out.println("updatePhotoAlbumWithDataType_1");
        if (i>0){
            resultMap.put("status","success");
            resultMap.put("code",1);
            resultMap.put("message","更新成功");
            System.out.println("updatePhotoAlbumWithDataType_2");
        }else {
            resultMap.put("status","failed");
            resultMap.put("code",0);
            resultMap.put("message","更新失败");
            System.out.println("updatePhotoAlbumWithDataType_3end");
        }
        return resultMap;
    }

    //根据id查寻图册
    public List<TAtlasInfo> queryPhotoInByIds(List<Integer> Ids){

        String IDs = StringUtils.join(Ids, ",");
        List<TAtlasInfo> tAtlasInfos = tCodePhotoAlbumMapper.queryPhotoInByIds(IDs);
         System.out.println("queryPhotoInByIds");
        return tAtlasInfos;
    }

    //根据名称查寻图册
    public List<TAtlasInfo> queryPhotoInByName(String name){
         System.out.println("queryPhotoInByName");
        return tCodePhotoAlbumMapper.queryPhotoInByName(name);
    }

    //新增图册
    public void savePhotoAlbum(TAtlasInfo info){
         System.out.println("savePhotoAlbum");
        tCodePhotoAlbumMapper.savePhotoAlbum(info);

    }

    //更改图册信息
    @Transactional(rollbackFor = Exception.class)
    public void updatePhotoAlbum(TAtlasInfo info) throws Exception {
        File file = null;
        String fileUrl = info.getFileUrl();
         System.out.println("updatePhotoAlbum_1");
        if (StringUtils.isNotBlank(fileUrl)) {
            file = new File(info.getFileUrl());
            System.out.println("updatePhotoAlbum_2");
        }
        if (file != null && file.exists()) {
            System.out.println("updatePhotoAlbum_3");
            String fileSuf = fileUrl.substring(fileUrl.indexOf(".") + 1);
            String pdfPath = fileUrl.substring(0, fileUrl.indexOf(".")) + ".pdf";
            /**
             * 区分是什么文件类型
             */
            if (fileSuf.equalsIgnoreCase("doc") || fileSuf.equalsIgnoreCase("docx")) {
                System.out.println("updatePhotoAlbum_4");
                WordToPdfUtil.wordToPdf(fileUrl, pdfPath);
            }
            if (fileSuf.equalsIgnoreCase("ppt") || fileSuf.equalsIgnoreCase("pptx")) {
                System.out.println("updatePhotoAlbum_5");
                FileToPdfUtils.pptToWord(fileUrl, pdfPath);
            }
            System.out.println("updatePhotoAlbum_6");
            info.setFileUrl(fileUrl.replace(resourceLocations, ""));
        }
        System.out.println("updatePhotoAlbum_7end");
        tCodePhotoAlbumMapper.updatePhotoAlbum(info);

    }


    //删除图册信息
    public Map<String,Object> deletePhotoAlbum(String id){

        //定义返回值
        Map<String,Object> resultMap = new HashMap<>();

        int i = tCodePhotoAlbumMapper.queryDataIsExit(id);
        int i1 = tCodePhotoAlbumMapper.queryImageIsExit(id);
        int i2 = tCodePhotoAlbumMapper.queryVideoIsExit(id);

        System.out.println(i+i1+i2);
 System.out.println("deletePhotoAlbum_1");
        if (i+i1+i2>0){
            resultMap.put("status","failed");
            resultMap.put("code",2);
            resultMap.put("message","不可删除");
            System.out.println("deletePhotoAlbum_2");
        }else {

          //先查询进行物理删除
          List<TAtlasInfo> tAtlasInfos = tCodePhotoAlbumMapper.queryPhotoInByIds(id);
          //如果没找到数据不可删除
          if (tAtlasInfos==null||tAtlasInfos.size()==0){
            resultMap.put("status","failed");
            resultMap.put("code",2);
            resultMap.put("message","未找到数据，无法删除");
            return resultMap;
          }
          //進行物理刪除
          deleteFileByUrl(tAtlasInfos);
          int i3 = tCodePhotoAlbumMapper.deletePhotoAlbum(id);
            if (i3>0){
                resultMap.put("status","success");
                resultMap.put("code",1);
                resultMap.put("message","删除成功");
                System.out.println("deletePhotoAlbum_3");
            }else {
                resultMap.put("status","failed");
                resultMap.put("code",0);
                resultMap.put("message","删除失败");
                System.out.println("deletePhotoAlbum_4");
            }
        }
        System.out.println("deletePhotoAlbum_5end");
        return resultMap;
    }


  private void deleteFileByUrl(List<TAtlasInfo> tAtlasInfos){

    for (TAtlasInfo tAtlasInfo : tAtlasInfos) {
      String fileUrl1=resourceLocations+tAtlasInfo.getFileUrl();
      System.out.println("删除地址打印:"+fileUrl1);
      String dirUrl = fileUrl1.substring(0, fileUrl1.lastIndexOf("/"));
      boolean del = FileUtil.del(dirUrl);
       System.out.println("是否成功："+del);

    }
  }



}
